package Dailog;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.luck.picture.lib.decoration.RecycleViewDivider;

import java.util.List;

import student.andlei.com.student.R;
import view.WrapContentLinearLayoutManager;

/**
 * author : ChenHuiMing
 * date : 2018/9/26
 * email : 907399255@qq.com
 * description : 支付Dialog
 */
public class ListDialog extends PopupWindow {

    private final BaseQuickAdapter<String, BaseViewHolder> mAdapter;
    private RecyclerView mRecyclerView;
    private TextView mTvTitle;
    private Context mContext;

    public ListDialog(@NonNull Context context, String title, List<String> list) {
        super(new LinearLayout(context), WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT, true);
        this.mContext = context.getApplicationContext();
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setOutsideTouchable(true);
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        setContentView(R.layout.dialog_list);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mRecyclerView = (RecyclerView) findViewById(R.id.recyclerView);
        mTvTitle.setText(title);
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new RecycleViewDivider(
                context, LinearLayoutManager.HORIZONTAL, 1, context.getResources().getColor(R.color.gray)));

        mRecyclerView.setAdapter(mAdapter = new BaseQuickAdapter<String, BaseViewHolder>(R.layout.item_dialog_list, list) {

            @Override
            protected void convert(BaseViewHolder helper, String item) {
                helper.setText(R.id.tv_item, item);
            }
        });

        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (mOnListItemClickListener != null) {
                    mOnListItemClickListener.onItemClick(position, mAdapter.getItem(position));
                }
            }
        });
    }

    public void setContentView(int layoutId) {
        View contentView = LayoutInflater.from(mContext).inflate(layoutId, (ViewGroup) getContentView(), false);
        LinearLayout linearLayout = new LinearLayout(mContext);
        linearLayout.addView(contentView);
        setContentView(linearLayout);
        setWidth(contentView.getLayoutParams().width);
        setHeight(contentView.getLayoutParams().height);
    }

    public View findViewById(int id) {
        return getContentView().findViewById(id);
    }


    private OnListItemClickListener mOnListItemClickListener;

    public void setListItemClickListener(OnListItemClickListener onListItemClickListener) {
        mOnListItemClickListener = onListItemClickListener;
    }

    public interface OnListItemClickListener {
        public void onItemClick(int position, String s);
    }
}
